package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.xqyy.eval.dao.IQuestionDao;
import org.xqyy.eval.dao.ITestPaperDao;
import org.xqyy.eval.dao.TQuestionMapper;
import org.xqyy.eval.model.Question;
import org.xqyy.eval.model.TQuestion;
import org.xqyy.eval.service.IQuestionService;

import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-24.
 */

@Component
@Transactional
public class QuestionService implements IQuestionService {

    @Autowired
    private IQuestionDao questionDao;
    @Autowired
    private ITestPaperDao testPaperDao;
    @Autowired
    private TQuestionMapper questionMapper;

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void add(Map<String, Object> params) {
        questionDao.add(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void modify(Map<String, Object> params) {
        questionDao.modify(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(Map<String, Object> params) {
        questionDao.delete(params);
    }


    @Override
    public List<Map<String, Object>> load(Map<String, Object> params) {
        String paperId = (String) params.get("paperId");
        if (StringUtils.hasLength(paperId)) {
            Map<String, Object> testPaper = testPaperDao.loadOne(params);
            if (testPaper != null) {
                String type = (String) testPaper.get("type");
                params.put("type", type);
            }
        }
        return questionDao.load(params);
    }

    @Override
    public List<Map<String, Object>> getpaperId(String code){
        return testPaperDao.getpaperId(code);
    }

    @Override
    public List<Map<String, Object>> getgroupCode(String code){
        return testPaperDao.getgroupCode(code);
    }

    /**
     * @param paperId 试卷Id
     * @return
     */
    @Override
    public List<TQuestion> selectQuestionByPaperId(Integer paperId) {
        return questionMapper.selectQuestionByPaperId(paperId);
    }

    /**
     * 查询所有问题
     *
     *  * @return
     */
    @Override
    public List<TQuestion> selectAllQuestion() {
        return questionMapper.selectAll();
    }

    /**
     * 添加问题
     *
     * @param tQuestion
     * @return
     */
    @Override
    public Boolean addQuestion(TQuestion tQuestion) {
        tQuestion.setType("A");
        questionMapper.insert(tQuestion);
        return true;
    }

    /**
     * 查询一个问题
     *
     * @param tQuestion
     * @return
     */
    @Override
    public TQuestion findQuestionById(TQuestion tQuestion) {
        return questionMapper.selectByPrimaryKey(tQuestion.getId());
    }

    /**
     * 修改问题
     *
     * @param tQuestion
     * @return
     */
    @Override
    public Boolean updateQuestion(TQuestion tQuestion) {
        questionMapper.updateByPrimaryKey(tQuestion);
        return true;
    }
}
